package com.ceres.customer.controller;

import com.ceres.customer.pojo.Role;
import com.ceres.customer.service.CustRoleService;
import com.ceres.entity.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/custRole")
public class CustRoleController {

    @Autowired
    private CustRoleService custRoleService;

    /**
     * 角色添加方法
     *
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, Integer[] menuIds,@RequestBody Role role) {
        try {
            custRoleService.add(role, permissionIds, menuIds);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 角色修改方法
     *
     * @param role
     * @return
     */
    @RequestMapping("/update")
    public Result update(Integer[] permissionIds, Integer[] menuIds,@RequestBody Role role) {
        try {
            custRoleService.update(role, permissionIds, menuIds);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 角色删除方法
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(int id) {
        try {
            custRoleService.delete(id);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 分页模糊查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(String keyword, int pageNum, int pageSize) {
        try {
            Page findPage = custRoleService.findPage(keyword, pageNum, pageSize);
            return new Result(true, 2000, findPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 通过id查询角色
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id) {
        try {
            Role role = custRoleService.findById(id);
            return new Result(true, 2000, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> roleList = custRoleService.findAll();
            return new Result(true, 2000, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 根据用户id获取对应角色
     * @param id
     * @return
     */
    @GetMapping("/findByUserId")
    public Result findByUserId(int id) {
        try {
            List<Role> roleList = custRoleService.findByUserId(id);
            return new Result(true, 2000, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }
}
